<?php

class Score_Plugin_Backroute extends Zend_Controller_Plugin_Abstract {

    // Fuer das Modul Backdoor soll ein anders Layout verwendet werden, außerdem sollen Unterseiten nur berechtigten Benutzern zugaenglich sein

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        if ((('analytical' == $request->getControllerName()) || ('event' == $request->getControllerName()) || ('game' == $request->getControllerName()) || ('location' == $request->getControllerName()) || ('account' == $request->getControllerName()) ) &&
                (!Zend_Auth::getInstance()->hasIdentity() && !('login' == $request->getActionName() || 'register' == $request->getActionName() || 'check-login' == $request->getActionName() ))) {
            $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
            $this->_errorMessenger->setNamespace('error');
            $this->_errorMessenger->addMessage(('You must be logged in'));
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
            $redirector->gotoUrl('/account/login');
        }
        if ((Zend_Auth::getInstance()->hasIdentity()) && ('login' == $request->getActionName() || 'register' == $request->getActionName())) {
            $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
            $this->_errorMessenger->setNamespace('error');
            $this->_errorMessenger->addMessage(('Hm?!'));
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
            $redirector->gotoUrl('/index');
        }


        if ('backdoor' == $request->getModuleName()) {
            $layout = Zend_Layout::getMvcInstance();
            $layout->setLayout('backdoor-layout');
            $view = $layout->getView();
            $view->headTitle('Management');
            $view->headLink()->appendStylesheet('/resources/css/backdoor.css');

            // Alle Request die nicht angemldet sind werden auf die loginseite umgelenkt


                if (Zend_Auth::getInstance()->getIdentity()->admin != '1'

                    && !('check-login' == $request->getActionName()
                    || 'login' == $request->getActionName())
            ) {
                $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
                $redirector->gotoUrl('/backdoor/index/login');
            }
        }
    }

}
